

class DBasePannel extends DBaseCom{

    public _KEY:string;
    public _RES:string[] = [];
    public _loadedRes:string[] = [];

    private _loaded:boolean = false;
    private _started:boolean = false;
    private _attached = {};
    private _destroyed:boolean = false;

    private _fairyguiPackage:string;
    private _fairyguiResName:string;
    protected _fairyguiView:fgui.GComponent;

    private _tabIndex:number = -1;
    constructor(){
        super();
    }

    public get loaded():boolean{
        return this._loaded;
    }

    public get started():boolean{
        return this._started;
    }

    protected get view():fgui.GComponent{
        return this._fairyguiView;
    }

    protected init(){}
    public setTableIndex(index:number, ... args){
        if(!this.loaded){ this._tabIndex = index; return; }
        this.updateTableIndex(index, args)
    }
    public get tableIndex():number{ return this._tabIndex; }

    public updateTableIndex(index:number, ... args){

    }

    public async start(onProcess?:Function){
        if(this.started){
            return;
        }
        this._started = true;
        if(this._RES.length > 0){
            await DResMgr.load(this._RES, onProcess, this);
            this._loadedRes = this._RES;
            egret.setTimeout(this.delayInit, this, 1);
        }else{
            egret.setTimeout(this.delayInit, this, 1);
        }
    }

    public async preStart(){
        if(this.started){
            return;
        }
        this._started = true;
        egret.setTimeout(this.delayInit, this, 1);
    }

    protected delayInit(){
        if(this._destroyed){
            return;
        }
        this._loaded = true;
        if(this._fairyguiPackage){
            let fui = RES.getRes(this._fairyguiPackage + DConfig.packageExtend);
            fgui.UIPackage.addPackage(this._fairyguiPackage, fui);
            let paths = this._fairyguiPackage.split("/");
            let package_name = paths[paths.length - 1];
            this._fairyguiView = fgui.UIPackage.createObject(
                package_name, this._fairyguiResName).asCom;
            this.addChild(this._fairyguiView);

            this._fairyguiView.width = DGame.stageWidth;
            this._fairyguiView.height = DGame.stageHeight;
        }
        this.init();
        let index = this._tabIndex;
        this._tabIndex = -1;
        this.setTableIndex(index);
        if(this.isShow){
            this.onShow();
        }
    }
    public show():DBasePannel{
        if(this.isShow){
            return;
        }
        this.isShow = true;
        if(this._loaded){
            this.onShow();
        }
        return this;
    }

    public hide(){
        if(!this.isShow){
            return;
        }
        this.isShow = false;
        this.onHide();
        DGame.onHidePannel(this);
    }

    public attach(type:number, fn:Function){
        if(type == 0){ return; }
        let id = DEvent.attach(type, fn, this);
        let idMap = this._attached[type];
        if(!idMap){
            this._attached[type] = idMap = {}; 
        }
        idMap[id] = id;
    }

    public detach(type:number, fn:Function){
        if(type == 0){ return; }
        let idMap = this._attached[type];
        if(idMap){
            let id = DEvent.detach(type, fn, this);
            delete idMap[id];
        }
    }

    public notify(type:number, data?:any){
        if(type == 0){ return; }
        DEvent.notify(type, data);
    }

    public get destroyed():boolean{
        return this._destroyed;
    }

    public destroy(){
        if(this._destroyed){
            return;
        }
        this._destroyed = true;
        if(!this.loaded){
            return;
        }
        DResMgr.release(this._loadedRes);
        for(var type in this._attached){
           let idMap = this._attached[type];
           for(var id in idMap){
               DEvent.detachId(type, Number(id));
           }
        }
        if(this.parent){
            this.hide();
        }
    }

    public setViewPackage(package_:string, resName:string, atlasCount:number){
        this._fairyguiPackage = DConfig.assetsPath + package_;
        this._fairyguiResName = resName;
        this.addViewPackage(package_, atlasCount);
    }

    public addViewPackage(package_:string, atlasCount:number){
        this._RES.push(DConfig.assetsPath + package_ + DConfig.packageExtend);
        for(let i = 0; i < atlasCount; i ++){
            this._RES.push(DConfig.assetsPath + package_ + "_atlas" + i + ".png");
        }
    }

    public setBasicViewPackage(resName:string){
        this._fairyguiPackage = DConfig.basicPackage;
        this._fairyguiResName = resName;
    }

}
